package com.elitel.frame.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.DownLoadJarService;
import com.elitel.frame.main.dao.BaseJarinfoMapper;
import com.elitel.frame.main.dao.BaseJarmethodMapper;
import com.elitel.frame.main.entity.BaseJarinfo;
import com.elitel.frame.main.entity.BaseJarmethod;
import com.elitel.frame.main.entity.ext.Census;

@Service
public class DownLoadJarServiceImpl implements DownLoadJarService {
	
	@Resource
	private BaseJarinfoMapper baseJarinfoMapper;
	
	@Resource
	
	private BaseJarmethodMapper baseJarmethodMapper;
	
	@Override
	public List<Census> getLoadJar() {
		List<Census> list = new ArrayList<>();
		List<Census> listb = new ArrayList<>();
		List<Census> listc = new ArrayList<>();
		List<Census> listd = new ArrayList<>();
		List<BaseJarinfo> jarInfoList = baseJarinfoMapper.selectAll();
		for(BaseJarinfo b:jarInfoList){
			Census census = new Census();
			census.setPid(String.valueOf(0));
			census.setName(b.getJarName());
			census.setId(b.getGuid());
			int a =1;
			List<String> packageList = baseJarmethodMapper.selectPackageList(b.getGuid());
			for(String bJarmethod :packageList){
				Census c = new Census();
				c.setId(bJarmethod);
				c.setName(bJarmethod);
				c.setPid(b.getGuid());
				List<BaseJarmethod> list4 = baseJarmethodMapper.selectClassMethod(bJarmethod); 
				for(BaseJarmethod bs:list4){
					Census censuss =  new Census();
					censuss.setId(bs.getClassName());
					censuss.setName(bs.getClassName());
					censuss.setPid(c.getId());
					List<Census> lista = new ArrayList<>();
					List<BaseJarmethod> list3 = baseJarmethodMapper.selectJarMethoding(b.getGuid(),bs.getClassName());
					for(BaseJarmethod ba :list3){
						Census census2 = new Census();
						census2.setId(ba.getClassMethod());
						census2.setPid(ba.getClassName());
						census2.setName(ba.getClassMethod());
						lista.add(census2);
					}
					censuss.setChildren(lista);
					listb.add(censuss);
				}
				c.setChildren(listb);
				listc.add(c);
			}
			census.setChildren(listc);
			listd.add(census);
		}
//		for(Census cc :list1){
//			List<BaseJarmethod> packageList = baseJarmethodMapper.selectPackageList(cc.getId());
//			for(BaseJarmethod bJarmethod :packageList){
//				Census c = new Census();
//				c.setId(bJarmethod.getPackageName());
//				c.setName(bJarmethod.getPackageName());
//				c.setPid(cc.getId());
//				list.add(c);
//				List<BaseJarmethod> list4 = baseJarmethodMapper.selectClassMethod(bJarmethod.getPackageName()); 
//				for(BaseJarmethod b:list4){
//					Census census =  new Census();
//					census.setId(b.getClassName());
//					census.setName(b.getClassName());
//					census.setPid(c.getId());
//					list.add(census);
//					List<BaseJarmethod> list3 = baseJarmethodMapper.selectJarMethoding(b.getClassName());
//					for(BaseJarmethod ba :list3){
//						Census census2 = new Census();
//						census2.setId(ba.getClassMethod());
//						census2.setPid(b.getClassName());
//						census2.setName(ba.getClassMethod());
//						list.add(census2);
//					}
//				}
//			}
//		}
		return listd;
	}

	@Override
	public int insertBaseJarInfo(BaseJarinfo baseJarinfo) {
		int a = baseJarinfoMapper.insertSelective(baseJarinfo);
		return a;
	}

	@Override
	public int insertBaseJarMethod(List<BaseJarmethod> list2) {
		int a = baseJarmethodMapper.insertBaseJarMethod(list2);
		return a;
	}

	@Override
	public int insertBaseJarMethodOne(BaseJarmethod baseJarmethod) {
		int a = baseJarmethodMapper.insertSelective(baseJarmethod);
		return a;
	}

	@Override
	public int deleteJar(String guid) {
		int a = 0;
		try {
			a = baseJarinfoMapper.deleteByPrimaryKey(guid);
			if (a>0) {
				baseJarmethodMapper.delectJarMethod(guid);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return a;
	}
	
}
